<?php
class buyer extends Admin {


    public function __construct() {
        parent::__construct();
        $this->type = G( 'a' ) == 'index' ? '' : G( 'a' );
    }

    public function index( $c, $a ) {

        $this->table = 'customer';

        $where = array( G( 'k' ) => G( 'v' ) );
        if ( $key = G( 'key' ) ) {
            $tmp = '';
            foreach ( $where as $k => $v ) {
                $tmp .= "{$k}='" . urldecode( $v ) . "' and ";
            }
            $where = $tmp ? ( $tmp . "wwId like '%{$key}%'" ) : '';
        }

        $this->pk = 'id';
        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $result = $result[ 'result' ];

        include $this->display( $c ); // 加载模板
    }

    public function lookChildren( $c, $a ) {

        $this->table = 'children';

        $where = array( G( 'k' ) => G( 'v' ) );
        if ( $key = G( 'key' ) ) {
            $tmp = '';
            foreach ( $where as $k => $v ) {
                $tmp .= "{$k}='" . urldecode( $v ) . "' and ";
            }
            $where = $tmp ? ( $tmp . "wwId like '%{$key}%'" ) : '';
        }

        $this->pk = 'id';
        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $result = $result[ 'result' ];

        include $this->display( $c ); // 加载模板
    }

    public function search($c,$a)
    {
        $this->table = 'customer'; ;
        $conditions = array( 'id' => $_GET['wwId'] );
        $result = $this->find($conditions);

        if(count($result) > 1)
        {
            $pager = $result['page'];
            $result = $result['result'];
        }
    }

    public function edit( $c, $a ) {
        if ( !$id = intval( G( 'id' ) ) ) {
            $this->error();
            exit;
        }
        $this->table = 'customer'; // 数据库表名
        $result = $this->find( array( 'id' => $id ) ); // 查找数据
        include $this->display( $c ); // 加载模板
    }
    public function editChildren( $c, $a ) {
        if ( !$id = intval( G( 'id' ) ) ) {
            $this->error();
            exit;
        }
        $this->table = 'customer'; // 数据库表名
        $result = $this->find( array( 'id' => $id ) ); // 查找数据
        include $this->display( $c ); // 加载模板
    }

    # 删除商品
    public function del( $c ) {
        if ( !$id = intval( G( "id" ) ) ) {
            exit( '非法删除商品' );
        }

        #删除商品
        $this->table = "customer";
        $msg = $this->delete( array( "id" => $id ) ) ? 'success' : 'failed';


        exit( $msg );
    }

    # 删除商品
    public function delChildren( $c ) {
        if ( !$id = intval( G( "id" ) ) ) {
            exit( '非法删除商品' );
        }

        #删除商品
        $this->table = "children";
        $msg = $this->delete( array( "id" => $id ) ) ? 'success' : 'failed';


        exit( $msg );
    }

    public function add( $c, $a ) {
        include $this->display( $c );
    }
    public function addChildren( $c, $a ) {
        include $this->display( $c );
    }

    public function saveChildren($c) {

        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        # 注意 表单字段必须跟数据库表字段名一样
        //$this->table = ''; // 数据库表名
        $this->table = "children"; // 数据库表名
        if ( $id = intval( G( "id" ) ) ) {
            $msg = $this->update( array( 'id' => $id ), $rows ) ? 'success' : 'failed'; //更新供货商
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed'; // 添加供货商
        }
        exit( $msg );
    }

    public function save($c) {

        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            //$pos = strpos($key, "businessType");
            if($key == 'businessType')
            {
                $string=implode(glue,$val);
                $rows[ $key ] = htmlspecialchars( trim( $string ) );
                continue;
            }
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        # 注意 表单字段必须跟数据库表字段名一样
        //$this->table = ''; // 数据库表名
        $this->table = "customer"; // 数据库表名
        if ( $id = intval( G( "id" ) ) ) {
            $msg = $this->update( array( 'id' => $id ), $rows ) ? 'success' : 'failed'; //更新供货商
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed'; // 添加供货商
        }
        exit( $msg );
    }

    public function get_date() {
        echo date( G( 'format', $this->setting[ 'timeformat' ] ) );
    }

}